#!/bin/bash

echo "Starting build process..."

# Set variables
FRONTEND_DIR="web"
BACKEND_DIR="app"
STATIC_DIR="$BACKEND_DIR/src/main/resources/static"

# Build frontend
echo "Building frontend..."
cd "$FRONTEND_DIR" || exit 1
npm install
if [ $? -ne 0 ]; then
    echo "Frontend dependency installation failed"
    exit 1
fi
npm run build
if [ $? -ne 0 ]; then
    echo "Frontend build failed"
    exit 1
fi
cd ..

# Clean and copy static resources
echo "Cleaning static resource directory..."
rm -rf "$STATIC_DIR"
mkdir -p "$STATIC_DIR"

echo "Copying frontend build files..."
cp -r "$FRONTEND_DIR/dist/"* "$STATIC_DIR"

# Build backend
echo "Building backend..."
mvn clean package -DskipTests

echo "Build completed!" 